package com.example.sharding.controller;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sharding.dto.UserVo;
import com.example.sharding.mapper.TeacherMapper;
import com.example.sharding.mapper.UserMapper;
import com.example.sharding.model.entity.Teacher;
import com.example.sharding.model.entity.User;
import com.github.javafaker.Faker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.scene.control.Pagination;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/api/test")
public class Test {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @RequestMapping("/getUsers")
    @ResponseBody
    public String getUsers(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.between("create_time", DateUtil.parse("2024-01-01 00:00:00"), DateUtil.parse("2024-03-01 00:00:00"));
        List<User> list =userMapper.selectList(userQueryWrapper);

        return String.valueOf(list.size());
    }

    @RequestMapping("/insert")
    @ResponseBody
    public String insert(){
        User user = new User();

        Random random = new Random();
        int randomNumber = random.nextInt(100) + 1;

        user.setName("test"+randomNumber);
        user.setSex("男");

        Faker faker = new Faker();
        Calendar fromDate = Calendar.getInstance();
        fromDate.set(2024, Calendar.JANUARY, 1);
        Calendar toDate = Calendar.getInstance();
        toDate.set(2024, Calendar.DECEMBER, 31);
        user.setCreateTime(faker.date().between(fromDate.getTime(), toDate.getTime()));

        userMapper.insert(user);

        return "success";
    }

    @RequestMapping("/getPage/{current}")
    @ResponseBody
    public String getPage(@PathVariable("current") int current){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.between("create_time", DateUtil.parse("2024-01-01 00:00:00"), DateUtil.parse("2024-05-01 00:00:00"));
        userQueryWrapper.orderByDesc("create_time");
        //List<User> list =userMapper.selectList(userQueryWrapper);

        Date s = DateUtil.parse("2024-01-01 00:00:00");
        Date e = DateUtil.parse("2024-05-01 00:00:00");
        int count = userMapper.getCount(s, e);

        int start = (current-1) * 10 + 1;
        //List<User> users = userMapper.getPage("2024-01-01 00:00:00", "2024-05-01 00:00:00", 1, 10);

        //Page<User> page = new Page<>(current, count, false);
        //Page<User> users = userMapper.selectPage(page, userQueryWrapper);

        Page page = PageHelper.startPage(current, 10, false);
        page.setOrderBy("create_time desc");

        List<User> list = userMapper.selectList(userQueryWrapper);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(count);
        return JSON.toJSONString(pageInfo);
    }

    @RequestMapping("/getCount")
    @ResponseBody
    public String getCount(){
        Date s = DateUtil.parse("2024-01-01 00:00:00");
        Date e = DateUtil.parse("2024-05-01 00:00:00");
        List<UserVo> res = userMapper.selectUser(s, e);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/getTeacher")
    @ResponseBody
    public String getTeacher(){
        QueryWrapper<Teacher> userQueryWrapper = new QueryWrapper<>();
        List<Teacher> list =teacherMapper.selectList(userQueryWrapper);

        return JSON.toJSONString(list);
    }


    @RequestMapping("/insertTeacher")
    @ResponseBody
    public String insertTeacher(){
        Teacher teacher = new Teacher();
        teacher.setId(UUID.randomUUID().toString());
        Random random = new Random();
        int randomNumber = random.nextInt(100) + 1;
        teacher.setUsername("test"+randomNumber);
        teacher.setCode("c" + randomNumber);
        teacherMapper.insert(teacher);
        return "success";
    }
}
